# encoding: utf-8
include ActionView::Helpers::NumberHelper

module ImprimirOrdenCompra

  def self.get_params()

  end

  def self.draw(orden_compra)
    # Create a new document
    pdf = Prawn::Document.new(:page => "LETTER")
    sletra = 16
    
    pdf.instance_eval do
      width_image = 130
       height_image = 150
       pdf.image "#{Rails.root}/app/assets/images/image001.jpg", :width => width_image,:height=>height_image , :at => [200, cursor]
       draw_text "Orden de Compra", :at => [400, 690], :size => sletra, :style =>:bold
       move_down 50 
       tabla1(orden_compra.id)
       move_down 70
       tablaProveedor(orden_compra.proveedor)
       move_down 10
       total = 0
       headerTDetalle
       orden_compra.detalles_orden_compra.each do |detalle|
         unless detalle == orden_compra.detalles_orden_compra.last
          tablaDetalle(detalle,20,false)
         else
           tablaDetalle(detalle,130,true)
         end
        total += detalle.Importe unless detalle.Importe.nil?
       end
       
      move_down 10
      tablaDetalleTotal(total)
      move_down 10
      tablaTelefonos
       

      render
    end
  end
    
end

def tabla1(no_orden)
  fecha = DateTime.now.strftime("%d/%m/%Y").to_s
  data = [[{:content =>"Fecha"},{:content =>"No Orden"}],
          [{:content => fecha }, {:content => no_orden.to_s}]

         ]
  table(data ,:position => :right, 
      :column_widths => {0 => 70 ,1 => 70},
        :cell_style => { :font => "Times-Roman",:size => 8, :min_font_size => 8}  
  ) 
end

def tablaProveedor(proveedor)
  datos = "JICHASA \n 7189-B Merchant Ave. \n El Paso, Tx 7991 5"
  
  datos_p = proveedor.NombreRazon + "\n" + proveedor.Direccion + "\n" + 
            proveedor.Ciudad + "," + proveedor.Estado + " " + 
            proveedor.CodigoPostal.to_s
  
  data = [[{:content =>"Proveedor"},{:content => "", :borders => [:right, :left]}, {:content => "Enviar a"}],
          [{:content => datos_p },{:content => "", :borders => [:right, :left]}, {:content => datos}]

         ]
  table(data ,:position => :center, 
      :column_widths => {0 => 230, 1 => 50 , 2 => 230},
        :cell_style => { :font => "Times-Roman",:size => 12, :min_font_size => 12}  
  ) 
end

def headerTDetalle
  tamanio_detalle = 280

  data = [[{:content =>"Item"},{:content => "Descripción"}, 
           {:content => "UM"}, {:content => "Precio"}, {:content => "Cantidad"}, {:content => "Importe"}]

         ]
  table(data ,:position => :center, 
      :column_widths => {0 => 45, 1 => 225 , 2 => 60, 3 => 60 , 4 => 60, 5 => 60 },
        :cell_style => { :font => "Times-Roman",:size => 12, :min_font_size => 12}  
  ) 
end

def tablaDetalle(detalle,tamanio_detalle,borde)
 #   data = [[{:content =>evento.nombre }],[{:content => "TOTAL DE VENTA: " + number_to_currency(total_venta(boletos), separator: ".", delimiter: ",") }]
  borde = borde == true ? [:right, :left , :bottom] : [:right, :left]
  data = [
          [{:content => detalle.item.Nombre, :height => tamanio_detalle , :borders => borde},
           {:content => detalle.item.DescripcionCompras, :height => tamanio_detalle , :borders => borde }, 
           {:content => detalle.item.UnidadMedida.Descripcion, :height => tamanio_detalle, :borders => borde  }, 
           {:content => number_to_currency(detalle.Precio) , :height => tamanio_detalle , :borders => borde}, 
           {:content => detalle.Cantidad.to_s,:height => tamanio_detalle , :borders => borde},
           {:content => number_to_currency(detalle.Importe),:height => tamanio_detalle , :borders => borde}
           ]

         ]
  table(data ,:position => :center, 
      :column_widths => {0 => 45, 1 => 225 , 2 => 60, 3 => 60 , 4 => 60, 5 => 60 },
        :cell_style => { :font => "Times-Roman",:size => 10, :min_font_size => 10}  
  ) 
end

def tablaDetalleTotal(total)
  total = total.nil? ? "0" : total.to_s

  data = [[{:content =>""},{:content => "TOTAL         " + number_to_currency(total)}]

         ]
  table(data ,:position => :center, 
      :column_widths => {0 => 330, 1 => 180 },
        :cell_style => { :font => "Times-Roman",:size => 12, :min_font_size => 12}  
  ) 
end


def tablaTelefonos

t_detalle = 20
  data = [[{:content =>"Teléfono #", :height => t_detalle},{:content => "Fax #", :height => t_detalle},{:content => "E-mail"}],
          [{:content =>""},{:content => ""},{:content => ""}]

         ]
  table(data ,:position => :center, 
      :column_widths => {0 => 170, 1 => 170 , 2 => 170},
        :cell_style => { :font => "Times-Roman",:size => 12, :min_font_size => 12}  
  ) 
end